/**
 * 起重机械设备craneDeviceList.js
 * 版本号： v1.0.0
 */
$(function () {
    /**
     *起重机械设备列表
     */
    var prId = $("#projectId_").val();
    var lastChecked = null;
    $("#craneDevice_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        border: false,
        url: CONTEXT_PATH + '/craneRecordData/craneRecordList.do?prId='+prId,
        idField: 'vpId',
        frozenColumns: [[
            {field: 'cpId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'project', title: '使用项目部', fixed:true,width: 100,formatter:function(val){
                        if(val){
                            return val.prName;
                        }
                    }},
                {field: 'cpName', title: '设备名称',fixed:true, width: 120,fixed:true},
                {field: 'csVer', title: '设备型号',fixed:true, width: 100,fixed:true,formatter:function(val,row){
                        if(row){
                            return row.craneStandard.csVer;
                        }
                    }},
                {field: 'propRecordNum', title: '产权备案号',fixed:true, width: 135},
                {field: 'meStatusCommonDict', title: '设备状态', width: 70,fixed:true,formatter:function(val){
                        if(val){
                            return "<span style='color: green; font-weight: bold'>"+val.propertyName+"</span>";
                        }
                    }},
                {field: 'torque', title: '额定力矩',fixed:true, width: 70,formatter:function(val,row){
                        if(row){
                            return row.craneStandard.torque;
                        }
                    }},
                {field: 'capacity', title: '额定吊重',fixed:true, width: 70,formatter:function(val,row){
                        if(row){
                            return row.craneStandard.capacity;
                        }
                    }},
                {field: 'liftHeight', title: '最大高度',fixed:true, width: 70,formatter:function(val,row){
                        if(row){
                            return row.craneStandard.liftHeight;
                        }
                    }},
                {field: 'maxRange', title: '最大幅度',fixed:true, width: 70,formatter:function(val,row){
                        if(row) {
                            return row.craneStandard.maxRange;
                        }
                    }},
                {field: '_operate', title: '操作', width: 60,
                    formatter:function(val,row,index){
                        // 设备在线情况才能查看实时数据
                        if(row.onLineStatusCommonDict && row.onLineStatusCommonDict.propertyCode == "1"){
                            return '<a href="#" onclick="deviceDesc('+index+')"><font style="color: #0E89CF">查看</font></a>';
                        }
                    }
                }
            ]
        ],
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#craneDevice_dataList").datagrid("selectRow", 0);
            }
        },
        onCheck:function(rowIndex, rowData){
            var row = $("#craneDevice_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#craneDevice_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        }
    });
});

/**
 * 查看具体设备详细参数
 * @param index
 */
function deviceDesc(index){
    $('#craneDevice_dataList').datagrid('selectRow',index);
    var row = $('#craneDevice_dataList').datagrid('getSelected');
    var html =
        '<div class="detail-left" style="width: 40%;margin: 0.4%;border: #8e9191 1px solid;display: inline-block;height: 97%;">' +
            '<div class="detail-left-left" style="width: 48%;float: left ;height: 97%;">' +
                '<p style="text-align: center;vertical-align:middle;line-height: 40px; font-size: 14px;font-weight: bold">布点位置：'+ row.distributLoc + '</p>' +
                '<p><img src="'+ CONTEXT_PATH +'/html/images/qizhongjixie.png" style="margin:auto;clear: both;display: block;"></p>' +
                '<p style="text-align: center;vertical-align:middle;line-height: 30px; font-size: 12px;font-weight: bold">设备号：'+ row.cpCode + '</p>' +
                '<p style="text-align: center;vertical-align:middle;line-height: 30px; font-size: 12px;font-weight: bold">设备状态：'+ row.onLineStatusCommonDict.propertyName + '</p>' +
                '<p id="collectTime" style="text-align: center;vertical-align:middle;line-height: 30px; font-size: 12px;font-weight: bold">获取时间：</p>' +
            '</div>' +
            '<div class="detail-left-right" style="width: 50%;float: left;height: 97%;">' +
                '<p style="text-align: center;vertical-align:middle;line-height: 40px; font-size: 14px;font-weight: bold">基本信息</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">使用项目:' + row.project.prName + '</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">设备名称:' + row.cpName + '</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">产权备案号:' + row.propRecordNum + '</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">设备状态:' + row.meStatusCommonDict.propertyName + '</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">额定力矩:' + row.craneStandard.torque + 'KN·m</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">额定吊重:' + row.craneStandard.capacity + 'T</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">最大高度:' + row.craneStandard.liftHeight + 'M</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">最大幅度:' + row.craneStandard.maxRange + 'M</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">大臂长:' + row.craneStandard.forearmLen + 'M</p>' +
                '<p style="text-align: left ;vertical-align:middle;line-height: 22px; font-size: 12px">平衡臂长:' + row.craneStandard.upperArm + 'M</p>' +
            '</div>' +
        '</div>' +
        '<div class="detail-right" style="width: 57.5%;margin: 0.4%;border: #8e9191 1px solid;display: inline-block;height: 97%;">' +
            '<div class="dataBar" style="float: left; width: 45%;margin-left: 3%;height: 70px;margin-top: 3px;">' +
                '<div class="dataDesc" style="height: 40%"><p style="line-height: 25px;">' +
                    '<span class="tabs-inner dataDesc" style="float: left; width: 100px;position: absolute;"><span class="tabs-icon icon-crane-torque"></span><span class="tabs-title tabs-with-icon" style="margin-left: 10px;">力矩</span></span> ' +
                    '<span class="dataToShow" id="momentPercent" style="float: right; margin-right: 10px;"></span> ' +
                '</p></div>' +
                '<div class="dy-progressbar"></div>' +
            '</div>' +
            '<div class="dataBar" style="float: left; width: 45%;margin-left: 3%;height: 70px;margin-top: 3px;">' +
                '<div class="dataDesc" style="height: 40%"><p style="line-height: 25px;">' +
                '<span class="tabs-inner dataDesc" style="float: left; width: 100px;position: absolute;"><span class="tabs-icon icon-crane-maxRange"></span><span class="tabs-title tabs-with-icon" style="margin-left: 10px;">幅度</span></span> ' +
                '<span class="dataToShow" id="radius" style="float: right; margin-right: 10px;"></span> ' +
                '</p></div>' +
                '<div class="dy-progressbar"></div>' +
            '</div>' +
            '<div class="dataBar" style="float: left; width: 45%;margin-left: 3%;height: 70px;margin-top: 3px;">' +
                '<div class="dataDesc" style="height: 40%"><p style="line-height: 25px;">' +
                '<span class="tabs-inner dataDesc" style="float: left; width: 100px;position: absolute;"><span class="tabs-icon icon-crane-capacity"></span><span class="tabs-title tabs-with-icon" style="margin-left: 10px;">吊重</span></span> ' +
                '<span class="dataToShow" id="loadData" style="float: right; margin-right: 10px;"></span> ' +
                '</p></div>' +
                '<div class="dy-progressbar"></div>' +
            '</div>' +
            '<div class="dataBar" style="float: left; width: 45%;margin-left: 3%;height: 70px;margin-top: 3px;">' +
                '<div class="dataDesc" style="height: 40%"><p style="line-height: 25px;">' +
                '<span class="tabs-inner dataDesc" style="float: left; width: 100px;position: absolute;"><span class="tabs-icon icon-crane-windSpeed"></span><span class="tabs-title tabs-with-icon" style="margin-left: 10px;">风速</span></span> ' +
                '<span class="dataToShow" id="windSpeed" style="float: right; margin-right: 10px;"></span> ' +
                '</p></div>' +
                '<div class="dy-progressbar"></div>' +
            '</div>' +
            '<div class="dataBar" style="float: left; width: 45%;margin-left: 3%;height: 70px;margin-top: 3px;">' +
                '<div class="dataDesc" style="height: 40%"><p style="line-height: 25px;">' +
                '<span class="tabs-inner dataDesc" style="float: left; width: 100px;position: absolute;"><span class="tabs-icon icon-crane-height"></span><span class="tabs-title tabs-with-icon" style="margin-left: 10px;">高度</span></span> ' +
                '<span class="dataToShow" id="height" style="float: right; margin-right: 10px;"></span> ' +
                '</p></div>' +
                '<div class="dy-progressbar"></div>' +
            '</div>' +
            '<div class="dataBar" style="float: left; width: 45%;margin-left: 3%;height: 70px;margin-top: 3px;">' +
                '<div class="dataDesc" style="height: 40%"><p style="line-height: 25px;">' +
                '<span class="tabs-inner dataDesc" style="float: left; width: 100px;position: absolute;"><span class="tabs-icon icon-crane-angle"></span><span class="tabs-title tabs-with-icon" style="margin-left: 10px;">回转角度</span></span> ' +
                '<span class="dataToShow" id="angle" style="float: right; margin-right: 10px;"></span> ' +
                '</p></div>' +
                '<div class="dy-progressbar"></div>' +
            '</div>' +
        '</div>';
    if(row){
        $('#detailWindow').window({
            title:"实时数据查看",
            width:800,
            height:400,
            modal:true,
            onBeforeOpen:function(){
                $('#detailWindow').html(html);
                var craneStandard = {
                    "torque" : row.craneStandard.torque,
                    "capacity": row.craneStandard.capacity,
                    "liftHeight" : row.craneStandard.liftHeight,
                    "maxRange" : row.craneStandard.maxRange,
                    "forearmLen" : row.craneStandard.forearmLen,
                    "upperArm" : row.craneStandard.upperArm,
                    "maxWindSpeed" : row.craneStandard.maxWindSpeed
                }
                $('#detailWindow').data("craneStandard", craneStandard);
                $(".dy-progressbar").dyProgressBar();
                startGetData(row);
            },
            onClose:function(){
                clearInterval(window.getDataTimer);
            }
        });
        $('#detailWindow').window("center");
    }
}

function startGetData(row){
    getData(row);
    var getDataTimer = setInterval(function(){getData(row);}, 10000);
    window.getDataTimer = getDataTimer;
}

function getData(row){
    $.ajax({
        url:CONTEXT_PATH + "/apiUtil/doGet.do",
        data:{
            url: "http://store.d.guijianyun.com:8000/devices/"+ row.deviceId +"/latest"
        },
        type:"get",
        success:function(data){
            if(data){
                try{
                    var jsonObj = $.parseJSON( data );
                    var craneStandard = $('#detailWindow').data("craneStandard");
                    if(!jsonObj.data){
                        return;
                    }
                    var collectTime = jsonObj.data.CollectionTime;
                    var angle = jsonObj.data.Angle;
                    var radius = jsonObj.data.Radius;
                    var loadData = jsonObj.data.LoadData;
                    var height = jsonObj.data.Height;
                    var momentPercent = jsonObj.data.MomentPercent;
                    var windSpeed = jsonObj.data.WindSpeed;
                    if (angle){
                        $("#angle").html(angle + " °");
                        var percent = angle / 360 *100;
                        if(percent < 0){
                            percent = 0
                        }else if(percent > 100){
                            percent = 100
                        }
                        $("#angle").parent().parent().next(".dy-progressbar").dyProgressBar("show", percent);
                    }else{
                        $("#angle").html("暂无数据");
                        $("#angle").parent().parent().next(".dy-progressbar").dyProgressBar("show", 0);
                    }
                    if (radius){
                        $("#radius").html(radius + " M");
                        var percent = radius / craneStandard.maxRange * 100;
                        if(percent < 0){
                            percent = 0
                        }else if(percent > 100){
                            percent = 100
                        }
                        $("#radius").parent().parent().next(".dy-progressbar").dyProgressBar("show", percent);
                    }else{
                        $("#radius").html("暂无数据");
                        $("#radius").parent().parent().next(".dy-progressbar").dyProgressBar("show", 0);
                    }
                    if (loadData){
                        $("#loadData").html(loadData + " T");
                        var percent = loadData / craneStandard.capacity * 100;
                        if(percent < 0){
                            percent = 0
                        }else if(percent > 100){
                            percent = 100
                        }
                        $("#loadData").parent().parent().next(".dy-progressbar").dyProgressBar("show", percent);
                    }else{
                        $("#loadData").html("暂无数据");
                        $("#loadData").parent().parent().next(".dy-progressbar").dyProgressBar("show", 0);
                    }
                    if (height){
                        $("#height").html(height + " M");
                        var percent = height / craneStandard.liftHeight * 100;
                        if(percent < 0){
                            percent = 0
                        }else if(percent > 100){
                            percent = 100
                        }
                        $("#height").parent().parent().next(".dy-progressbar").dyProgressBar("show", percent);
                    }else{
                        $("#height").html("暂无数据");
                        $("#height").parent().parent().next(".dy-progressbar").dyProgressBar("show", 0);
                    }
                    if (momentPercent){
                        $("#momentPercent").html(momentPercent + " %");
                        var percent = momentPercent / craneStandard.torque * 100;
                        if(percent < 0){
                            percent = 0
                        }else if(percent > 100){
                            percent = 100
                        }
                        $("#momentPercent").parent().parent().next(".dy-progressbar").dyProgressBar("show", percent);
                    }else{
                        $("#momentPercent").html("暂无数据");
                        $("#momentPercent").parent().parent().next(".dy-progressbar").dyProgressBar("show", 0);
                    }
                    if (windSpeed){
                        $("#windSpeed").html(windSpeed +  " M/s");
                        var percent = windSpeed / craneStandard.maxWindSpeed * 100;
                        if(percent < 0){
                            percent = 0
                        }else if(percent > 100){
                            percent = 100
                        }
                        $("#windSpeed").parent().parent().next(".dy-progressbar").dyProgressBar("show", percent);
                    }else{
                        $("#windSpeed").html("暂无数据");
                        $("#windSpeed").parent().parent().next(".dy-progressbar").dyProgressBar("show", 0);
                    }
                    $("#collectTime").html(collectTime ? "获取时间：" + collectTime : "获取时间: --:--:--");
                }catch (e) {
                    console.log(e.toString());
                }
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });
}


